/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.StorageUnit;
import org.crystalrange.enterprise.service.StorageUnitService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class StorageUnitListController extends BaseController {

    protected StorageUnitService storageUnitService;
    protected StorageUnit selectedStorageUnit;
    protected Listbox listboxStorageUnit;
    protected Search searchStorageUnit;
    protected List<StorageUnit> storageUnits;

    public StorageUnitService getStorageUnitService() {
        return this.storageUnitService;
    }

    public void setStorageUnitService(StorageUnitService storageUnitService) {
        this.storageUnitService = storageUnitService;
    }

    public StorageUnit getSelectedStorageUnit() {
        return this.selectedStorageUnit;
    }

    public void setSelectedStorageUnit(StorageUnit selectedStorageUnit) {
        this.selectedStorageUnit = selectedStorageUnit;
    }

    public List<StorageUnit> getStorageUnits() {
        return this.storageUnits;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchStorageUnit = new Search(StorageUnit.class);
        this.searchStorageUnit.setMaxResults(100);
        this.searchStorageUnit.addFetch("warehouseNumber");
        this.searchStorageUnit.addFetch("storageType");
        this.searchStorageUnit.addFetch("storageBin");
        this.searchStorageUnit.addFetch("storageUnitType");
        this.searchStorageUnit.addFetch("createdBy");
        this.searchStorageUnit.addFetch("updatedBy");
        this.storageUnits = this.storageUnitService.search(this.searchStorageUnit);

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemStorageUnitCreate(Event event) throws Exception {
        this.storageUnitCreate();
    }

    public void onClick$menuitemStorageUnitEdit(Event event) throws Exception {
        this.storageUnitEdit();
    }

    public void onClick$menuitemStorageUnitDelete(Event event) throws Exception {
        this.storageUnitDelete();
    }

    public void onClick$menuitemStorageUnitSearch(Event event) throws Exception {
        this.storageUnitSearch();
    }

    public void onClick$menuitemStorageUnitRefresh(Event event) throws Exception {
        this.storageUnitRefresh();
    }

    public void onClick$toolbarbuttonStorageUnitCreate(Event event) throws Exception {
        this.storageUnitCreate();
    }

    public void onClick$toolbarbuttonStorageUnitEdit(Event event) throws Exception {
        this.storageUnitEdit();
    }

    public void onClick$toolbarbuttonStorageUnitDelete(Event event) throws Exception {
        this.storageUnitDelete();
    }

    public void onClick$toolbarbuttonStorageUnitSearch(Event event) throws Exception {
        this.storageUnitSearch();
    }

    public void onClick$toolbarbuttonStorageUnitRefresh(Event event) throws Exception {
        this.storageUnitRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.storageUnitEdit();
    }
    
    private void storageUnitCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/storageUnit.zul");
    }

    private void storageUnitEdit() throws Exception {
        if (this.selectedStorageUnit != null) {
            this.execution.sendRedirect("~./zul/html/storageUnit.zul?Id=" + this.selectedStorageUnit.getId().toString());
        }
    }

    private void storageUnitDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxStorageUnit.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    StorageUnit storageUnit = (StorageUnit) selectedItem.getValue();
                    this.storageUnitService.remove(storageUnit);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.storageUnits = this.storageUnitService.search(this.searchStorageUnit);
        this.binder.loadAll();
    }

    private void storageUnitSearch() throws Exception {
        this.execution.setAttribute("searchStorageUnit", this.searchStorageUnit);

        Window window = (Window) this.execution.createComponents("v/storageUnitSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchStorageUnit = (Search) this.execution.getAttribute("searchStorageUnit");
        }

        this.storageUnits = this.storageUnitService.search(this.searchStorageUnit);
        this.binder.loadAll();
    }

    private void storageUnitRefresh() throws Exception {
        this.storageUnits = this.storageUnitService.search(this.searchStorageUnit);
        this.binder.loadAll();
    }
}
